package com.promotores;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.promotores.jsonparsing.JSONParser;
import com.promotores.util.Parametros;

public class JsonAdminxTipoEvento {
	// url to make request
	private static String url = "http://200.16.7.112:3000/promoter_management/promoters/administradores_por_tipo_evento/";
			
	// JSON Node names
	private static final String TAG_EVENT_TYPE_WORKER = "event_type_worker";
	private static final String TAG_CREATED_AT = "created_at";
	private static final String TAG_UPDATED_AT = "updated_at";
	private static final String TAG_ID = "id";
	private static final String TAG_EVENT_TYPE = "event_type";
	private static final String TAG_TIPO="tipo";
	
	JSONArray tipoeventos = null;
	
	public List<BeanTipoEvento> read(){
		List<BeanTipoEvento> lstTipoEventos = new ArrayList<BeanTipoEvento>();
				
		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();
		//String strUrl =url+Parametros.getCurrent_user()+"/"+Parametros.getLast_update()+"(yyyy-MM-dd'T'HH:mm:ss'Z').json";
		String strUrl =url+Parametros.getCurrent_user()+"/02-02-02.json";
		String readValues = jParser.read(strUrl);
		System.out.println("Json0: "+readValues);
		try {
			JSONArray jsonArray = new JSONArray(readValues);
			
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);			
							
				// Storing each json item in variable				
				int id = jsonObject.getJSONObject(TAG_EVENT_TYPE_WORKER).getInt(TAG_ID);
				String created_at = jsonObject.getJSONObject(TAG_EVENT_TYPE_WORKER).getString(TAG_CREATED_AT);
				String updated_at = jsonObject.getJSONObject(TAG_EVENT_TYPE_WORKER).getString(TAG_UPDATED_AT);
				int event_type_id = jsonObject.getJSONObject(TAG_EVENT_TYPE_WORKER).getJSONObject(TAG_EVENT_TYPE).getInt(TAG_ID);
				String event_type_tipo = jsonObject.getJSONObject(TAG_EVENT_TYPE_WORKER).getJSONObject(TAG_EVENT_TYPE).getString(TAG_TIPO);
				//falta sacar las zonas
				
				BeanTipoEvento tipoEvento = new BeanTipoEvento();
				tipoEvento.setId(event_type_id);
				tipoEvento.setTipoEvento(event_type_tipo);
				lstTipoEventos.add(tipoEvento);
			}
		} catch (Exception e) {
			System.out.println("Json: Exception");
			e.printStackTrace();	
			return null;
		}
		return lstTipoEventos;
	}		
}
